<template>
<h1>员工列表练习</h1>
  <input type="text" placeholder="姓名" v-model="emp.name"><br>
  <input type="text" placeholder="工资" v-model="emp.salary"><br>
  <input type="text" placeholder="工作" v-model="emp.job"><br>
  <input type="button" value="添加" @click="add()">
  <br><br>
  <table border="1" align="center">
    <caption>员工列表</caption>
    <tr>
      <th>编号</th>
      <th>姓名</th>
      <th>工资</th>
      <th>工作</th>
      <th>操作</th>
    </tr>
    <tr v-for="(emp,i) in empArr">
      <td>{{i+1}}</td>
      <td>{{emp.name}}</td>
      <td>{{emp.salary}}</td>
      <td>{{emp.job}}</td>
      <td><input type="button" value="删除" @click="del(i)"></td>
    </tr>
  </table>
</template>

<script setup>
import {ref} from "vue";

const empArr = ref([{name:"刘备",salary:5000,job:"销售"},
  {name:"关羽",salary:7000,job:"人事"},
  {name:"张飞",salary:9000,job:"程序员"}
]);
const emp = ref({name:"",salary:"",job:""});
const add = () =>{
  // 创建一个新的员工对象,添加到数组中
  let newEmp = {...emp.value}
  empArr.value.push(newEmp);
}
const del = (i) =>{
  if(confirm("您确认删除此员工吗?")){
    empArr.value.splice(i,1);
  }
}
</script>

<style scoped>

</style>